const JwtStrategy = require('passport-jwt').Strategy
const ExtractJwt = require('passport-jwt').ExtractJwt
const models = require('./../db/mysqldb/index')
const opts = {}
const config = require('./../config')
const _ = require('lodash')
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken()
opts.secretOrKey = config.secretOrKey

module.exports = (passport) => {
  passport.use(
    new JwtStrategy(opts, async function (jwtPayload, done) {
      try {
        // const { url } = req.url
        // console.log(url);
        const { role_code } = jwtPayload
        if (role_code && role_code !== config.SUPER_ROLE_CODE) {
          // 获取请求地址
          return done(null, jwtPayload)
        } else {
          if (role_code === config.SUPER_ROLE_CODE) {
            return done(null, jwtPayload)
          } else {
            return false
          }
        }
      } catch (err) {
        console.log(err)
        return false
      }
    }),

  )
}
